package com.blt.govdata.controller;

import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TSysMenu;
import com.blt.govdata.model.vo.TSysMenuVo;
import com.blt.govdata.service.ITSysMenuService;
import com.blt.govdata.service.ITSysUserRoleService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单管理前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tSysMenu")
//todo 将所有的XXX修改为真实值
public class TSysMenuController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITSysMenuService service;

    @Autowired
    private ITSysUserRoleService userRoleService;





    /**
     * 菜单管理，菜单树
     */

    @PostMapping("/getManageMenus")

    public Map<String, Object> getManageMenus(@RequestParam Map<String, Object> params) {
        params.put("Pid", 0);
        List<TSysMenuVo> menus = this.service.selectVoMenusByPid(params);
        Iterator it = menus.iterator();
        while (it.hasNext()) {
            TSysMenuVo menu = (TSysMenuVo) it.next();
            params.put("Pid", menu.getId());
            menu.setChildren(this.service.selectVoMenusByPid(params));
        }
        logger.info("查询管理菜单");
        return ImmutableMap.of("status", "true", "data", menus);
    }

    /**
     * 添加菜单
     */
    @PostMapping("/addMenu")
    public Map<String, Object> addMenu(TSysMenu sysMenu) {
        if (null == sysMenu) return ImmutableMap.of("status", "false", "data", false);
        Integer Pid = sysMenu.getPid();
        if (null == Pid) sysMenu.setPid(0);
        if (null != Pid && 0 != Pid) {
            TSysMenu menuParent = new TSysMenu();
            menuParent.setId(Pid);
            menuParent.setHasChild(1);
            this.service.updateById(menuParent);
        }
        boolean result = this.service.insert(sysMenu);
        logger.info("添加菜单");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑菜单
     */
    @PostMapping("/editMenu")
    public Map<String, Object> editMenu(TSysMenu sysMenu) {
        if (null == sysMenu || null == sysMenu.getId()) return ImmutableMap.of("status", "true", "data", false);
        boolean result = this.service.updateById(sysMenu);
        logger.info("编辑菜单");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除菜单
     */
    @PostMapping("/deleteMenu")
    public Map<String, Object> deleteMenu(TSysMenu sysMenu) {
        if (null == sysMenu || null == sysMenu.getId()) return ImmutableMap.of("status", "true", "data", false);
        sysMenu.setIsDelete(Constants.IS_DELETED);
        boolean result = this.service.updateById(sysMenu);
        logger.info("删除菜单");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 根据Id查询菜单
     */
    @RequestMapping(value = "/selectMenuById", params = {"id"})
    public Map<String, Object> selectMenuById(String id) {
        TSysMenu menu = this.service.selectById(id);
        if (menu == null) return ImmutableMap.of("status", "flase", "msg", "未查询到菜单信息");
        return ImmutableMap.of("status", "true", "data", menu);
    }
}
